// ==UserScript==
// @name         豆包AI生图去水印
// @namespace    https://github.com/LauZzL/remove-doubao-images
// @version      1.0.0
// @description  通过hook掉JSON.parse实现豆包AI生图下载原图去水印!
// @author       LauZzL
// @match        https://www.doubao.com/*
// @icon         https://gitee.com/selling-surprises/Greasyfork-Script-repository/raw/main/%E5%90%84%E5%A4%A7%E5%B9%B3%E5%8F%B0/%E5%AD%97%E8%8A%82%E8%B7%B3%E5%8A%A8/%E8%B1%86%E5%8C%85/DouBao.png
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function findAllKeysInJson(obj, key) {
        const results = [];
        function search(current) {
            if (current && typeof current === 'object') {
                if (!Array.isArray(current) && Object.prototype.hasOwnProperty.call(current, key)) {
                    results.push(current[key]);
                }
                const items = Array.isArray(current) ? current : Object.values(current);
                for (const item of items) {
                    search(item);
                }
            }
        }
        search(obj);
        return results;
    }

    let _parse = JSON.parse;
    JSON.parse = function(data) {
        let jsonData = _parse(data);
        if (!data.match('creations')) return jsonData;
        let creations = findAllKeysInJson(jsonData, 'creations');
        if (creations.length > 0) {
            creations.forEach((creaetion) => {
                creaetion.map((item)=>{
                    const rawUrl = item.image.image_ori_raw.url;
                    item.image.image_ori.url = rawUrl;
                    //预览时也去水印
                    //item.image.image_preview.url = rawUrl;
                    //item.image.image_thumb.url = rawUrl;
                    return item;
                });
            })
        }
        return jsonData;
    }
})();